//
//  LoginViewController.m
//  NavZe
//
//  Created by ZengYuan on 15/8/3.
//  Copyright (c) 2015年 ZengYuan. All rights reserved.
//

#import "LoginViewController.h"
#import "AppDelegate.h"
#import "UserUnit.h"
#import "PhoneViewController.h"
#import "UserLogin.h"

@interface LoginViewController ()

@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    coverView.layer.cornerRadius = 3;
    coverView.layer.masksToBounds=YES;
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.titleLabel.text=@"登录";
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/


- (IBAction)forgetPasswd:(id)sender {
    
    UIStoryboard *board = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    PhoneViewController *evc = [board instantiateViewControllerWithIdentifier:@"phonevc"];
    evc.phone_func = FIND_PASSWD;
    
    [[NSUserDefaults standardUserDefaults] setObject:@"0" forKey:@"setpasswd_target"];

    
    [self.navigationController pushViewController:evc animated:YES];
    
}


- (IBAction)reg:(id)sender {
    
    UIStoryboard *board = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    PhoneViewController *evc = [board instantiateViewControllerWithIdentifier:@"phonevc"];
    evc.phone_func = REG_ACCOUNT;
    [[NSUserDefaults standardUserDefaults] setObject:@"1" forKey:@"setpasswd_target"];

    [self.navigationController pushViewController:evc animated:YES];

}

- (IBAction)loginAction:(id)sender {
    
    
    if ([self.userField.text length] == 0) {
        MBALERT(@"请输入手机号");
        return;
    }
    
//    NSString* rule1 = @"^1(3|5|7|8|4)\\d{9}";
//    NSPredicate* pred=[NSPredicate predicateWithFormat:@"SELF MATCHES %@",rule1];
//    BOOL isMatch=[pred evaluateWithObject:self.userField.text];
//    if(!isMatch){
//        MBALERT(@"手机号格式不对");
//        return;
//    }
    
    if ([self.passField.text length]==0) {
        MBALERT(@"请输入密码");
        return;
    }
    
//    Reachability*rea= [Reachability reachabilityForInternetConnection];
    if (![self networkAvailble]){
        MBALERT(HTTPERROR_CANNOTREACH);
        return;
    }
    
    UIWindow *window=[[UIApplication sharedApplication].windows lastObject];
    hud=[MBProgressHUD showHUDAddedTo:window animated:YES];
    hud.mode = MBProgressHUDModeIndeterminate;
    hud.labelText=@"登录中...";

    [UserLogin loginBy:self.userField.text andPwd:self.passField.text success:^{
        [hud hide:YES afterDelay:0.5];

        AppDelegate *delegate = [[UIApplication sharedApplication] delegate];
        [delegate setTabAfterLogin];
        
    } failure:^(NSString *error) {
        hud.mode = MBProgressHUDModeText;
        hud.labelText=error;
        [hud hide:YES afterDelay:0.5];
    }];
    
//    NSDictionary *dic = [[NSDictionary alloc] initWithObjects: @[self.userField.text,self.passField.text] forKeys:@[@"phoneno", @"secret"]];
//    
//    NSString *json  =[dic JSONString];
//    NSString *url = [NSString stringWithFormat:MAINURL,URL_LOGIN];
    

//    [AWHttpTool post:url body:json success:^(id json) {
//        if (json) {
//            [hud hide:YES afterDelay:0.5];
//
//            NSInteger sta = [json[@"status"] integerValue];
//            NSString *msg = json[@"msg"];
//            if (sta == 0) {
//                NSString *token = json[@"session_id"];
//                NSLog(@"session_id = %@",token);
//
//                [[NSUserDefaults standardUserDefaults]setObject:token forKey:@"session_id"];
//                
//                UserUnit *userunit =[[UserUnit alloc]init];
//                userunit.useruuid = self.userField.text;
//                userunit.userName = self.userField.text;
//                userunit.userPasswd = self.passField.text;
//                [UserUnit resetLoginUnit:userunit];
//                
//                AppDelegate *delegate = [[UIApplication sharedApplication] delegate];
//                [delegate setTabAfterLogin];
//                
//            }else{
//                hud.mode = MBProgressHUDModeText;
//                hud.labelText=msg;
//                [hud hide:YES afterDelay:0.5];
//            }
//        }
//    } failure:^(NSError *error) {
//        
////        [hud hide:YES afterDelay:0.5];
////        AppDelegate *delegate = [[UIApplication sharedApplication] delegate];
////        [delegate setTabAfterLogin];
////        return ;
//        
//        hud.mode = MBProgressHUDModeText;
//        hud.labelText=@"登录失败";
//        [hud hide:YES afterDelay:0.5];
//
//    }];
    
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}

@end
